package org.hero.web;

import com.github.pagehelper.PageHelper;
import org.hero.dao.NewsDao;
import org.hero.entity.CommunityInformation;
import org.hero.entity.JqGridPageView;
import org.hero.entity.QueryResult;
import org.hero.service.UserService;
import org.hero.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * Created by xuzhongyao on 2017/6/6.
 */
@Controller
public class PubParImpController {

    @Autowired
    NewsDao newsDao;

    @Autowired
    private UserService userService;

    @RequestMapping("/uploadExcel")
    @ResponseBody
    public boolean uploadExcel(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;//request强制转换注意
        boolean isok = false;
        List<MultipartFile> files = mRequest.getFiles("file");
        for (MultipartFile file : files) {
            isok = userService.impExcel(ExcelUtil.convert(file));
            System.out.println(isok);
        }
        return isok;
    }

    @RequestMapping(value = "/thump")
    public ModelAndView toImpPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/WEB-INF/thump.jsp");
        return mv;
    }

    @RequestMapping(value = "/selectPubPar")
    @ResponseBody
    public JqGridPageView<CommunityInformation> getPubPar(HttpServletRequest request){

        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));

        PageHelper.startPage(page, rows);
        List<CommunityInformation> list = newsDao.selectPubPar();

        QueryResult<CommunityInformation> queryResult = new QueryResult<CommunityInformation>();
        queryResult.setResultList(list);
        queryResult.setTotalCount(newsDao.selectPubParTotal());
        JqGridPageView<CommunityInformation> res = new JqGridPageView<CommunityInformation>();
        res.setCurrentPage(1);
        res.setMaxResults(20);
        res.setQueryResult(queryResult);

        return res;
    }

}
